package com.atguigu.tms.mock.service.impl;

import com.atguigu.mock.util.RandomNum;
import com.atguigu.mock.util.RandomNumString;
import com.atguigu.tms.mock.bean.BaseOrgan;
import com.atguigu.tms.mock.bean.BaseRegionInfo;
import com.atguigu.tms.mock.bean.TruckInfo;
import com.atguigu.tms.mock.bean.TruckTeam;
import com.atguigu.tms.mock.config.AppConfig;
import com.atguigu.tms.mock.mapper.TruckInfoMapper;
import com.atguigu.tms.mock.service.*;
import com.atguigu.tms.mock.service.adv.impl.AdvServiceImpl;
import org.apache.commons.lang3.RandomStringUtils;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 车辆信息表 服务实现类
 * </p>
 *
 * @author zhangchen
 * @since 2022-06-19
 */
@Service
public class TruckInfoServiceImpl extends AdvServiceImpl<TruckInfoMapper, TruckInfo> implements TruckInfoService {

    @Autowired
    TruckModelService truckModelService;

    @Autowired
    BaseRegionInfoService baseRegionInfoService;

    @Autowired
    BaseOrganService baseOrganService;

    @Autowired
    TruckTeamService truckTeamService;

    public TruckInfo initTruckInfo(Long orgId,Integer lineLevel){
        BaseOrgan baseOrgan=baseOrganService.getById(orgId,true);

        TruckInfo truckInfo = new TruckInfo();
        if(lineLevel.equals(1)){
            truckInfo.setTruckModelId(1L); //重卡
        }else{
            truckInfo.setTruckModelId(2L);//轻卡
        }

        Long orgCityId = baseOrgan.getRegionId();
        BaseRegionInfo baseRegionInfoCity = baseRegionInfoService.getById(orgCityId, true);
        BaseRegionInfo  baseRegionInfoProvince = baseRegionInfoService.getById(baseRegionInfoCity.getParentId(), true);

        String shortName =baseRegionInfoProvince.getShortName();

        truckInfo.setTruckNo(shortName+ RandomStringUtils.random(1,"ABCD")+RandomStringUtils.random(5,true,true).toUpperCase());
        truckInfo.setEngineNo(RandomStringUtils.random(7,true,true));
        truckInfo.setDeviceGpsId(RandomNumString.getRandNumString(0,9,12,"",true));
        truckInfo.setLicenseExpireDate(DateUtils.addDays(AppConfig.mock_date, RandomNum.getRandInt(100,300)));
        truckInfo.setLicenseRegistrationDate(DateUtils.addDays(AppConfig.mock_date,0- RandomNum.getRandInt(300,3000)));
        truckInfo.setLicenseLastCheckDate(DateUtils.addDays(AppConfig.mock_date,0- RandomNum.getRandInt(100,200)));


        TruckTeam truckTeam = truckTeamService.getById(baseOrgan.getId(),true);
        truckInfo.setTeamId(truckTeam.getId());

        truckInfo.setIsEnabled("1");
        truckInfo.setIsDeleted("0");
        truckInfo.setCreateTime(AppConfig.mock_date);

        saveOrUpdate(truckInfo,true);
        return  truckInfo;

    }
}
